package com.pay.cloud;

import android.content.Context;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;

import com.pay.cloud.service.NetworkService;

import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

public class Application extends android.app.Application {
    private static final String TAG = Application.class.getSimpleName();
    private static Application app;
    private static TextToSpeech speech;
    private Intent networkService;
    //private BroadcastReceiver reportReceiver;

    @Override
    public void onCreate() {
        super.onCreate();

        app = this;

        Context context = getApplicationContext();

        try {
            initSslSocketFactory(context);
        } catch (Exception e) {
            Log.d(TAG, "ssl证书配置失败: ", e);
            Toast.makeText(context, "ssl证书配置失败：" + e.getMessage(), Toast.LENGTH_LONG).show();
        }

        networkService = new Intent(context, NetworkService.class);
        startService(networkService);
        //reportReceiver = new ReportReceiver();
        //registerReceiver(reportReceiver, new IntentFilter(ReportReceiver.class.getName()));

    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        stopService(networkService);
        //unregisterReceiver(reportReceiver);
    }

    public static Context getContext() {
        return app.getApplicationContext();
    }

    public final static void speak(String text) {
        if (null != speech) {
            speech.shutdown();
        }
        speech = new TextToSpeech(getContext(), status -> speech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null));
    }

    public final static void speak(int textId) {
        speak(getContext().getString(textId));
    }

    private static void initSslSocketFactory(Context context) throws Exception {
        InputStream is = context.getAssets().open("-_cashier_qcloud_com.cer");

        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate cer = cf.generateCertificate(is);

        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(null, null);
        ks.setCertificateEntry("trust", cer);

        KeyManagerFactory kf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kf.init(ks, null);

        TrustManagerFactory tf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tf.init(ks);

        SSLContext sslContext = SSLContext.getInstance("TLS");// SSL
        sslContext.init(kf.getKeyManagers(), tf.getTrustManagers(), new SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    }
}
